package cn.yeziji.netty.config;

import cn.yeziji.netty.config.handler.NettyWebsocketHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * websocket 通道初始化配置类
 *
 * @author gzkemays
 * @since 2022/11/07 4:15 PM
 */
@Configuration
public class NettyWebsocketInit extends ChannelInitializer {
  @Resource NettyWebsocketHandler nettyWebsocketHandler;

  @Override
  protected void initChannel(Channel channel) throws Exception {
    ChannelPipeline pipeline = channel.pipeline();
    // 对http协议的支持.
    pipeline.addLast(new HttpServerCodec());
    // 对大数据流的支持
    pipeline.addLast(new ChunkedWriteHandler());
    // post请求分三部分. request line / request header / message body
    // HttpObjectAggregator将多个信息转化成单一的request或者response对象
    pipeline.addLast(new HttpObjectAggregator(8000));
    // 将http协议升级为ws协议. websocket的支持
    pipeline.addLast(new WebSocketServerProtocolHandler("/chat"));
    // 自定义处理handler
    pipeline.addLast(nettyWebsocketHandler);
  }
}
